# Create your views here.
from django.shortcuts import render_to_response
from django.utils import simplejson
from forms import LoginForm, JIDForm
import xmpp_lib

def login(request):
    context = {}
    if request.POST:
        form = LoginForm(request.POST)
        if form.is_valid():
            jid = form.cleaned_data['jid']
            password = form.cleaned_data['password']
            jabber_session = xmpp_lib.JabberSession(jid, password)

            return_context = {'uuid': jabber_session.uuid,
                              'roster': jabber_session.roster.getItems(),
                              }
            context['return_string'] = simplejson.dumps(return_context)
    
            return render_to_response('xmpp-psn/json.html', context)
        else:
            context['form'] = form
    else:
        context['form'] = LoginForm()
    
    return render_to_response('xmpp-psn/login.html', context)

def connect(request):
    username = request.POST['username']
    password = request.POST['password']
    
    xmpp_lib.JabberSession(username, password)


    return_context = {'uuid': jabber_session.uuid,
                      'roster': jabber_session.roster,
                     }
    context = {'return_string': simplejson.dumps(return_context)}
    
    return render_to_response('xmpp-psn/json.html', context)
    
def disconnect(request, uuid):
    return_code = xmpp_lib.workers[uuid].stop()
    context = {'return_code': return_code}
    
    return render_to_response('xmpp-psn/return_code.html', context)

def roster(request, uuid):
    """returns the roster in json format"""
    roster = xmpp_lib.workers[uuid].session.get_roster()
    return_string = simplejson.dumps({'roster': roster.getItems()})
    
    context = {'return_string': return_string,}
    return render_to_response('xmpp-psn/roster.html', context)

def subscribe(request, uuid, jid=None):
    """
    requires the jid you wish to subscribe to
    """
    if not jid:
        if request.POST:
            form = JIDForm(request.POST)
            if form.is_valid():
                jid = form.cleaned_data['jid']
            else:
                return render_to_response('xmpp-psn/submit.html', {'form': form})
        else:
            form = JIDForm()
            return render_to_response('xmpp-psn/submit.html', {'form': form})

    return_code = xmpp_lib.workers[uuid].session.subscribe(jid)
    
    context = {'return_code': return_code}
    return render_to_response('xmpp-psn/return_code.html', context)

def unsubscribe(request, uuid, jid):
    if not jid:
        if request.POST:
            form = JIDForm(request.POST)
            if form.is_valid():
                jid = form.cleaned_data['jid']
            else:
                return render_to_response('xmpp-psn/submit.html', {'form': form})
        else:
            form = JIDForm()
            return render_to_response('xmpp-psn/submit.html', {'form': form})

    uuid = uuid.encode('ascii')
    return_code = xmpp_lib.workers[uuid].session.unsubscribe(jid)
    
    context = {'return_code': return_code}
    return render_to_response('xmpp-psn/return_code.html', context)
    
def authorize(request, uuid, jid):
    if not jid:
        if request.POST:
            form = JIDForm(request.POST)
            if form.is_valid():
                jid = form.cleaned_data['jid']
            else:
                return render_to_response('xmpp-psn/submit.html', {'form': form})
        else:
            form = JIDForm()
            return render_to_response('xmpp-psn/submit.html', {'form': form})

    return_code = xmpp_lib.workers[uuid].session.authorize(jid)
    
    context = {'return_code': return_code}
    return render_to_response('xmpp-psn/return_code.html', context)

def unauthorize(request, uuid, jid):
    if not jid:
        if request.POST:
            form = JIDForm(request.POST)
            if form.is_valid():
                jid = form.cleaned_data['jid']
            else:
                return render_to_response('xmpp-psn/submit.html', {'form': form})
        else:
            form = JIDForm()
            return render_to_response('xmpp-psn/submit.html', {'form': form})

    return_code = xmpp_lib.workers[uuid].session.unauthorize(jid)
    
    context = {'return_code': return_code}
    return render_to_response('xmpp-psn/return_code.html', context)

def remove(request, uuid, jid):
    if not jid:
        if request.POST:
            form = JIDForm(request.POST)
            if form.is_valid():
                jid = form.cleaned_data['jid']
            else:
                return render_to_response('xmpp-psn/submit.html', {'form': form})
        else:
            form = JIDForm()
            return render_to_response('xmpp-psn/submit.html', {'form': form})

    session = xmpp_lib.workers[uuid].session

    return_code = session.remove(jid)

    print(jid)

    if return_code:
        return_string = simplejson.dumps(session.roster.getItems())
        context = {'return_string': return_string}
        template = 'xmpp-psn/json.html'
    else:
        context = {'return_code': return_code}
        template = 'xmpp-psn/return_code.html'
    return render_to_response(template, context)

def send(request, uuid, jid):
    if request.POST:
        message = request.POST['message']
    else:
        context = {'return_code': False}
    
    context = {'return_code': xmpp_lib.workers[uuid].session.send(jid, message)}
    
    return render_to_response('xmpp-psn/return_code.html', context)